<script setup>
import { ref } from "vue";
import { useRouter } from "vue-router";
import { fetchLogin } from "@/api/user.js";
const router = useRouter();
import { useUserStore } from "@/store";
const userStore = useUserStore();
import { ElMessage } from "element-plus";
const ruleForm = ref({
  username: "",
  password: "",
  level: "0",
});
const formRef = ref(null);
const loading = ref(false);
const rules = {
  name: [{ required: true, message: "用户名不能为空！", trigger: "blur" }],
  password: [{ required: true, message: "密码不能为空！", trigger: "blur" }],
};

const submitForm = async (formRef) => {
  try {
    const valid = await formRef.validate();
    if (valid) {
      const response = await fetchLogin(ruleForm.value);

      console.log(response.data.code);
      if (response.data.code == "200") {
        const permissions = response.data.Permissions;
        sessionStorage.setItem(
          "userRole",
          permissions === 1 ? "teacher" : "student"
        );
        sessionStorage.setItem("userInfo", JSON.stringify(response.data.data));

        if (response.data.data.level == 1) {
          router.push("/teacher/home");
        } else if (response.data.data.level == 0) {
          router.push("/student/home");
        }
        // router.push("/home");
        userStore.setUser(response.data.data);
        ElMessage.success(response.data.message); // 成功提示
      } else {
        ElMessage.warning(response.data.message); // 警告提示
      }
    }
  } catch (error) {
    console.error("Error during form submission:", error);
  } finally {
    loading.value = false; // 不管成功与否都结束加载动画
  }
};

const beforeTo = () => {
  router.push("/reset");
};
const form = ref({
  name: "",
});
</script>
<template>
  <div class="bj">
    <el-card class="box-card">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="formRef"
        label-position="left"
        label-width="70px"
        class="login-form"
      >
        <el-form-item label="用户名">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="ruleForm.password"
            autocomplete="off"
            clearable
            show-password
            @keyup.enter.native="submitForm('ruleForm')"
          />
        </el-form-item>
        <div style="margin-left: 20%">
          <el-radio-group v-model="ruleForm.level">
            <el-radio label="0">学生</el-radio>
            <el-radio label="1">管理员</el-radio>
          </el-radio-group>
        </div>
      </el-form>
      <div class="btnGroup" style="margin-top: 50px">
        <el-button
          type="primary"
          @click="submitForm(formRef)"
          v-loading="loading"
          class="denglu"
          >登录</el-button
        >
        <router-link to="/register">
          <el-button class="zhuche">注册</el-button>
        </router-link>
      </div>
    </el-card>
  </div>
</template>
<style scoped>
.bj {
  background-image: url("@/assets/init/LoginGround.jpg");
  background-size: cover;
  background-attachment: fixed;
  height: 100vh;
  display: flex;
  justify-content: right;
  align-items: center;
  padding-right: 10%;
}

.box-card {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 400px;
  height: 300px;
  border-radius: 10px;
  background-color: rgba(255, 255, 255, 0.7);
  box-shadow: 5px 5px 0 0 rgba(0, 0, 0, 0.2);
}

.login-from {
  margin: auto auto;
}

.zhaohui {
  float: right;
  text-decoration: none;
  color: #50b6ff;
  position: absolute;
  right: 1%;
  top: 88%;
}

.denglu {
  margin-right: 20%;
  margin-left: 20%;
}

.zhuche {
  margin-left: 10px;
}
</style>
